package com.okRPC.protocol;

import com.okRPC.serialization.SerializationTypeEnum;
import lombok.Data;

import java.io.Serializable;
import java.util.UUID;

/**
 * Description: This is a sample class.
 * Author: Simon
 * Date: 2023/10/10
 */
@Data
public class MessageHeader implements Serializable {
        /*
    +---------------------------------------------------------------+
    | 魔数 2byte | 协议版本号 1byte | 序列化算法 1byte | 报文类型 1byte  |
    +---------------------------------------------------------------+
    | 状态 1byte |        消息 ID 32byte     |      数据长度 4byte     |
    +---------------------------------------------------------------+
    */

    private short magic;

    private byte version;

    private byte serialization;

    private byte messageType;

    private byte messageStatus;

    private String requestId;

    private int len;

    public static MessageHeader build(String serialization) {
        MessageHeader messageHeader = new MessageHeader();
        messageHeader.setMagic(ProtocolConstant.MAGIC);
        messageHeader.setVersion(ProtocolConstant.VERSION);
        messageHeader.setRequestId(UUID.randomUUID().toString().replaceAll("-",""));
        messageHeader.setMessageType(MessageType.REQUEST.getType());
        messageHeader.setSerialization(SerializationTypeEnum.parseByName(serialization).getType());
        return messageHeader;
    }

}













